/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package AccManager;

/*standard import*/
import java.io.*;
import java.util.*;

import Common.InfoLogger;
/*
 * xml stuff
 */
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import org.xml.sax.helpers.DefaultHandler;
/**
 *
 * @author lcy
 */
public class ServerSAXParser extends DefaultHandler implements Runnable {

    private InfoLogger _debugger;

    List<UserProfile> GamerList;
    private String tempVal;

    private String _database_file_name;
    //to maintain context
    private volatile UserProfile temp_user_entry;

    //Constructor
    public ServerSAXParser(){
        _database_file_name = "employees.xml";
        InitializeCommComponents();
    }

    //Constructor
    public ServerSAXParser(String file_name){
        _database_file_name = file_name;
        InitializeCommComponents();
    }

    private void InitializeCommComponents()
    {
        GamerList = new LinkedList<UserProfile>();
        _debugger = new InfoLogger();
    }

    public void run() {
	parseDocument();
    }

    public List<UserProfile> parseDocument() {
        //get a factory
        SAXParserFactory spf = SAXParserFactory.newInstance();
        try {
            //get a new instance of parser
            SAXParser sp = spf.newSAXParser();
            //parse the file and also register this class for call backs
            sp.parse(_database_file_name, this);
        }catch(SAXException se) {
            se.printStackTrace();
            _debugger.LogInfo("SAX exception");
        }catch(ParserConfigurationException pce) {
            pce.printStackTrace();
            _debugger.LogInfo("Parser configuration exception");
        }catch (IOException ie) {
            ie.printStackTrace();
            _debugger.LogInfo("SAX Parser: IO exception");
        }

        return GamerList;
    }

    //Event Handlers
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
            //reset
            tempVal = "";
            if(qName.equalsIgnoreCase("Gamer")) {
                    //create a new instance of employee
                    temp_user_entry = new UserProfile();
            }
    }


    public void characters(char[] ch, int start, int length) throws SAXException {
            tempVal = new String(ch,start,length);
    }

    public void endElement(String uri, String localName, String qName) throws SAXException {

            if(qName.equalsIgnoreCase("Gamer")) {
                    //add it to the list
                    OrderedAddItem();
            }else if (qName.equalsIgnoreCase("Name")) {
                    temp_user_entry.SetUserID(tempVal);
                   
            }else if (qName.equalsIgnoreCase("Passwd")) {
                    temp_user_entry.SetPasswd(tempVal);
            }else if (qName.equalsIgnoreCase("AvatarID")){
                    temp_user_entry.setAvatarID(tempVal);
            }else if(qName.equalsIgnoreCase("ALevel")){
                    int Alevel = Integer.parseInt(tempVal);
                    temp_user_entry.setAlevel(Alevel);
            }else if(qName.equalsIgnoreCase("DLevel")){
                    int Dlevel = Integer.parseInt(tempVal);
                    temp_user_entry.setAlevel(Dlevel);
            }else if(qName.equalsIgnoreCase("Coins")){
                    int coins = Integer.parseInt(tempVal);
                    temp_user_entry.setCoins(coins);
            }else if(qName.equalsIgnoreCase("Exp")){
                    int exp = Integer.parseInt(tempVal);
                    temp_user_entry.setExp(exp);
            }else if(qName.equalsIgnoreCase("MP")){
                    int mp = Integer.parseInt(tempVal);
                    temp_user_entry.SetMagicPoint(mp);
            }else if(qName.equalsIgnoreCase("Capa")){
                    String [] list = tempVal.split(",");

                    for(int cnt = 0; cnt < list.length && list[cnt] != ""; cnt ++){
                       temp_user_entry.setCapa(cnt,Integer.parseInt(list[cnt]));
                    }
            }
            else if(qName.equalsIgnoreCase("Gender")){
                    int gender = Integer.parseInt(tempVal);
                    temp_user_entry.setGender(gender);
            }
            else if(qName.equalsIgnoreCase("Status")){
                    UserStatus status = GetStatus(tempVal);
                    temp_user_entry.SetUserStatus(status);
            }
            else if(qName.equalsIgnoreCase("Contact")){
                    String [] list = tempVal.split(",");

                    for(int cnt = 0; cnt < list.length && list[cnt] != ""; cnt ++){
                       temp_user_entry.InsertContact(list[cnt]);
                    }
            }
            else{
                //Do nothing
                System.out.println("Keyword wrong: " + qName);
            }

    }

    private UserStatus GetStatus(String value){
        if(value.equals("online")){
            return UserStatus.ONLINE;
        }
        else if(value.equals("offline")){
            return UserStatus.OFFLINE;
        }
        else if(value.equals("ingame")){
            return UserStatus.INGAME;
        }

        return UserStatus.NONEXIST;
       

    }

    private void OrderedAddItem(){
        for(int idx = 0; idx < GamerList.size(); idx ++){

            if(!temp_user_entry.IsBiggerThan(GamerList.get(idx),CompareRule.NAME)){
                GamerList.add(idx, temp_user_entry);
                return;
            }
        }

        GamerList.add(temp_user_entry);
    }

}
